package core;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.beans.PropertyVetoException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JInternalFrame;
import javax.swing.JTextArea;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;
import javax.swing.text.AbstractDocument;

public class PostIt extends JInternalFrame implements InternalFrameListener, MouseMotionListener, 
			Comparable<PostIt>{
	private JTextArea area; 
	private boolean select;
	private Point origin;
	private int r;
	private long timeCreated;
	public static final String DATE_FORMAT_NOW = "hh:mm:ss";
	
	public long getTimeCreated() {
		return timeCreated;
	}

	public void setTimeCreated(long timeCreated) {
		this.timeCreated = timeCreated;
	}

	public Point getOrigin() {
		return origin;
	}

	public void setOrigin(Point origin) {
		this.origin = origin;
	}

	public JTextArea getArea() {
		return area;
	}

	
	public PostIt(int x, int y) {
		r = 200;
		area = new JTextArea(5,10);
		setMaxCharacterLimit(60);
		setOrigin(new Point(x,y));
		
		//Set properties of the text area of the post-it
		area.setPreferredSize(new Dimension(120,120));
		area.setIgnoreRepaint(true);
		area.setBackground(Color.getHSBColor(60, 20, 20));
		area.setFont(new Font("Comic Sans MS", Font.PLAIN, 13));
		area.setBorder(BorderFactory.createLineBorder(Color.getHSBColor(60, 0, 20), 2));
		area.setEnabled(true);
		area.setEditable(true);
		area.setDragEnabled(true);
		area.setLineWrap(true);
		area.setWrapStyleWord(true);
		area.setMargin(new Insets(5,5,5,5));
		area.setName("PostIt");
		
		//Set properties of the internal frame holding the post it.
		this.setBounds(x, y, r, r);
		Calendar cal = Calendar.getInstance();
	    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
	    String ampmString = "am";
	    if(cal.get(Calendar.AM_PM)==1)
	    	ampmString = "pm";
		this.setTitle(sdf.format(cal.getTime())+" "+ampmString);
		this.setResizable(false);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.addInternalFrameListener(this);
		this.add(area);
		this.setTimeCreated(System.currentTimeMillis());
		this.setClosable(true);		
		try {
			this.setSelected(true);
		} catch (PropertyVetoException e) {
			e.printStackTrace();
		}
		this.setVisible(true);
	}
    
    public void mouseMoved(MouseEvent e) {
    }
    
    public void mouseDragged(MouseEvent e) {
    }
    
  //Set maximum character limit as maxChars
    public void setMaxCharacterLimit(int maxChars) {
		AbstractDocument pDoc=(AbstractDocument)area.getDocument();
		pDoc.setDocumentFilter(new core.DocumentSizeFilter(maxChars));
    }
    
    /** Calculate this node's rectangular boundary. */
    public void setBoundary(Rectangle b) {
        b.setBounds(origin.x - 10, origin.y - 10, 220, 220);
    }
    
    /** Return true if this node is select. */
    public boolean isSelect() {
        return select;
    }

    /** Mark this node as select. */
    public void setSelect(boolean select) {
        this.select = select;
    }
    
    /** Collected all the select nodes in list. */
    public static void getSelect(List<PostIt> list, List<PostIt> select) {
        select.clear();
        for (PostIt n : list) {
            if (n.isSelect()) {
                select.add(n);
            }
        }
    }


  /** Select each node in r. */
  public static void selectRect(List<PostIt> list, Rectangle r) {
      for (PostIt n : list) {    	  
          n.setSelect(r.intersects(n.origin.x, n.origin.y, 200, 200));
          if(n.select) {
        	  n.getArea().setBackground(Color.lightGray);
          }
          else {
        	  n.getArea().setBackground(Color.getHSBColor(60, 20, 20));
          }
      }
  }

  /** Toggle selected state of each node containing p. */
  public static void selectToggle(List<PostIt> list, Point p) {
      for (PostIt n : list) {
          if (n.contains(p)) {
              n.setSelect(!n.isSelect());
          }
      }
  }

@Override
public int compareTo(PostIt o) {
	if(o.getTimeCreated()<this.getTimeCreated())
		return -1;
	else if(o.getTimeCreated()>this.getTimeCreated())
		return 1;
	return 0;
}

@Override
public void internalFrameActivated(InternalFrameEvent arg0) {
	// TODO Auto-generated method stub

}

@Override
public void internalFrameClosed(InternalFrameEvent arg0) {
	// TODO Auto-generated method stub
}

@Override
public void internalFrameClosing(InternalFrameEvent arg0) {
	// TODO Auto-generated method stub
	BoardPane.getNodes().remove(arg0.getSource());
}

@Override
public void internalFrameDeactivated(InternalFrameEvent arg0) {
	System.out.println("Deactivate event fired");
	PostIt current = (PostIt)arg0.getSource();
	current.setOrigin(new Point(current.getX(), current.getY()));	
}

@Override
public void internalFrameDeiconified(InternalFrameEvent arg0) {
	// TODO Auto-generated method stub
	
}

@Override
public void internalFrameIconified(InternalFrameEvent arg0) {
	// TODO Auto-generated method stub
	
}

@Override
public void internalFrameOpened(InternalFrameEvent arg0) {
	// TODO Auto-generated method stub
	
}
}
